<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button size="small" type="primary" @click="onAdd(0)"
          >添加菜单</el-button
        >
      </div>
      <el-table
        border
        stripe
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
        row-key="menuId"
        highlight-current-row
        :default-expand-all="false"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="菜单名称" min-width="200">
          <template slot-scope="scope">
            <span>{{
              scope.row.menuName + "  [ " + scope.row.menuId + "  ]"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="route" label="菜单地址" min-width="150" />
        <el-table-column label="菜单图标" min-width="100">
          <template slot-scope="scope">
            <div class="listPic">
              <i :class="'el-icon-' + scope.row.icon" style="font-size: 20px" />
            </div>
          </template>
        </el-table-column>
        <!--<el-table-column-->
        <!--prop="status"-->
        <!--label="是否显示"-->
        <!--min-width="100"-->
        <!--&gt;-->
        <!--<template slot-scope="scope">-->
        <!--<el-switch-->
        <!--v-model="scope.row.status"-->
        <!--:active-value="1"-->
        <!--:inactive-value="0"-->
        <!--@change="onchangeIsShow(scope.row)"-->
        <!--/>-->
        <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column prop="createTime" label="创建时间" min-width="150" />
        <el-table-column label="操作" min-width="240" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              :disabled="isChecked"
              @click="onAdd(scope.row.menuId)"
              >添加子菜单</el-button
            >
            <el-button
              icon="el-icon-edit"
              type="warning"
              size="small"
              @click="onEdit(scope.row.menuId)"
            ></el-button>
            <el-button
              icon="el-icon-delete"
              type="danger"
              size="small"
              @click="handleDelete(scope.row.menuId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加菜单的对话框 -->
    <el-dialog
      title="添加菜单"
      :visible.sync="addDialogVisible"
      @close="addDialogClosed"
      width="50%"
    >
      <!-- 表单区域 -->
      <el-form
        :model="addRuleForm"
        :rules="rules"
        ref="addRuleFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 父级分类级联选择器 -->
        <el-form-item label="父级分类 :">
          <el-cascader
            v-model="selectedCateKEys"
            placeholder="请选择父级分类"
            clearable
            :options="tableData.data"
            :props="tableDatada"
            expand-trigger="hover"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <!-- 是否显示 -->
        <el-form-item label="是否显示 :" prop="hidden">
          <el-switch
            v-model="addRuleForm.hidden"
            active-color="#13ce66"
            inactive-color="#708090"
          >
          </el-switch>
        </el-form-item>
        <!-- 是否商户菜单 -->
        <el-form-item label="是否商户菜单 :" prop="isMer">
          <el-switch
            v-model="addRuleForm.isMer"
            active-color="#13ce66"
            inactive-color="#708090"
          >
          </el-switch>
        </el-form-item>
        <!-- 菜单图标 -->
        <el-form-item label="菜单图标 :" prop="icon">
          <el-input class="imput" v-model="addRuleForm.icon"></el-input>
          <el-button
            type="warning"
            class="el-icon-circle-plus-outline"
            @click="setIcon"
          ></el-button>
        </el-form-item>
        <!-- 菜单名称 -->
        <el-form-item label="菜单名称 :" prop="menuName">
          <el-input v-model="addRuleForm.menuName"></el-input>
        </el-form-item>
        <!-- 路由 -->
        <el-form-item label="路由名称 :" prop="route">
          <el-input v-model="addRuleForm.route"></el-input>
        </el-form-item>
        <!-- 排序 -->
        <el-form-item label="排序 :" prop="sort">
          <el-input-number
            v-model="addRuleForm.sort"
            :step="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑菜单的对话框 -->
    <el-dialog
      title="添加菜单"
      :visible.sync="editDialogVisible"
      @close="addDialogClosed"
      width="50%"
    >
      <!-- 表单区域 -->
      <el-form
        :model="editRuleForm"
        :rules="rules"
        ref="addRuleFormRef"
        label-width="100px"
      >
        <!-- 父级分类级联选择器 -->
        <el-form-item label="父级分类 :">
          <el-cascader
            v-model="selectedCateKEys"
            placeholder="请选择父级分类"
            clearable
            :options="tableData.data"
            :props="tableDatada"
            expand-trigger="hover"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <!-- 是否显示 -->
        <el-form-item label="是否显示 :" prop="hidden">
          <el-switch
            v-model="editRuleForm.hidden"
            active-color="#13ce66"
            inactive-color="#708090"
          >
          </el-switch>
        </el-form-item>
        <!-- 是否商户菜单 -->
        <el-form-item label="是否商户菜单 :" prop="isMer">
          <el-switch
            v-model="editRuleForm.isMer"
            active-color="#13ce66"
            inactive-color="#708090"
          >
          </el-switch>
        </el-form-item>
        <!-- 菜单图标 -->
        <el-form-item label="菜单图标 :" prop="icon">
          <el-input class="imput" v-model="editRuleForm.icon"></el-input>
          <el-button
            type="warning"
            class="el-icon-circle-plus-outline"
            @click="setIcon"
          ></el-button>
        </el-form-item>
        <!-- 菜单名称 -->
        <el-form-item label="菜单名称 :" prop="menuName">
          <el-input v-model="editRuleForm.menuName"></el-input>
        </el-form-item>
        <!-- 路由 -->
        <el-form-item label="路由名称 :" prop="route">
          <el-input v-model="editRuleForm.route"></el-input>
        </el-form-item>
        <!-- 排序 -->
        <el-form-item label="排序 :" prop="sort">
          <el-input-number
            v-model="editRuleForm.sort"
            :step="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 选择菜单图标对话框 -->
    <el-dialog
      title="请选择菜单图标"
      :visible.sync="iconDialogVisible"
      width="70%"
    >
      <iconfrom @iconDialog="iconDialog"></iconfrom>

      <span slot="footer" class="dialog-footer">
        <el-button @click="iconDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="iconDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  menuListApi,
  menuCreateApi,
  menuUpdateApi,
  menuDeleteApi,
  menuInfoApi,
} from "@/api/system";
import iconfrom from "@/components/iconFrom";
export default {
  name: "Menu",
  components: {
    iconfrom,
  },
  data() {
    return {
      isChecked: false,
      listLoading: true,
      // 获取到的菜单列表
      tableData: {
        data: [],
        total: 0,
      },
      // 级联选择框双向绑定的数组
      selectedCateKEys: [],
      // 级联选择框的配置对象
      tableDatada: {
        value: "menuId",
        label: "menuName",
        children: "children",
        checkStrictly: true,
      },
      // 获取菜单列表的参数
      tableFrom: {
        page: 1,
        limit: 5,
      },
      // 菜单图标对话框
      iconDialogVisible: false,
      // 添加菜单对话框默认显示状态
      addDialogVisible: false,
      editDialogVisible: false,

      addRuleForm: {
        // createTime:'',
        icon: "",
        hidden: "false",
        // level: '',
        // menuId: '',
        menuName: "",
        parentId: "0",
        route: "",
        sort: "",
        isMer: "false",
      },
      editRuleForm: {},
      rules: {
        menuName: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
        ],
        route: [{ required: true, message: "请输入菜单路径", trigger: "blur" }],
        routeName: [
          { required: true, message: "请输入路由名称", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 列表
    getList() {
      this.listLoading = true;
      menuListApi(this.tableFrom)
        .then((res) => {
          console.log(res, "123456");
          this.tableData.data = res.data;
          console.log(this.tableData, "123456");
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    // 级联选择器改变时触发该函数
    handleChange() {
      if (this.selectedCateKEys) {
        this.selectedCateKEys = this.selectedCateKEys[
          this.selectedCateKEys.length - 1
        ];
      } else {
        this.selectedCateKEys = 0;
      }
      console.log(this.selectedCateKEys);
    },
    // 点击按钮获取设置图标
    setIcon() {
      this.iconDialogVisible = true;
      // this.$router.push("/admin/setting/icons");
    },
    pageChange(page) {
      this.tableData.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableData.limit = val;
      this.getList();
    },
    // 添加菜单对话框关闭事件
    addDialogClosed() {
      this.$refs.addRuleFormRef.resetFields();
    },
    // 添加
    onAdd(id) {
      if (Number(id) > 0) {
        menuInfoApi(id)
          .then((res) => {
            this.selectedCateKEys = res.data.menuId;
            this.addDialogVisible = true;
          })
          .catch((message) => {
            this.$message.error(message);
          });
      } else {
        (this.selectedCateKEys = []), (this.addDialogVisible = true);
      }
    },
    // 添加对话框提交按钮
    addSubmit() {
      if (this.selectedCateKEys.length !== 0) {
        this.addRuleForm.parentId = this.selectedCateKEys;
      } else {
        this.addRuleForm.parentId = 0;
      }
      console.log(this.addRuleForm);
      menuCreateApi(this.addRuleForm)
        .then((message) => {
          this.$message.success(message);
          this.addDialogVisible = false;
          this.getList();
        })
        .catch((message) => {
          this.$message.error(message);
          this.addDialogVisible = false;
        });
    },
    // 编辑对话框提交按钮
    editSubmit() {
      menuUpdateApi(this.editRuleForm.menuId, this.editRuleForm)
        .then((message) => {
          this.$message.success(message);
          this.editDialogVisible = false;
          this.getList();
        })
        .catch((message) => {
          this.$message.error(message);
          this.editDialogVisible = false;
        });
    },
    // 图标按钮对话框确认按钮
    iconDialog(n) {
      this.iconDialogVisible = false;
      console.log(n);
      this.addRuleForm.icon = n;
      this.editRuleForm.icon = n;
    },

    // 编辑
    onEdit(id) {
      menuInfoApi(id)
        .then((res) => {
          this.selectedCateKEys = res.data.menuId;
          this.editRuleForm = res.data;
          console.log(this.editRuleForm, "编辑");
          this.editDialogVisible = true;
        })
        .catch((message) => {
          this.$message.error(message);
        });
    },
    // 删除按钮点击事件
    async handleDelete(id) {
      // 弹框询问是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该菜单, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // 如果用户确认了删除，则返回值为字符串：confirm
      // 如果用户取消了删除，则返回值为字符串：cancel
      console.log(confirmResult);
      if (confirmResult !== "confirm") {
        this.$message.info("已经取消了删除");
      } else {
        menuDeleteApi(id)
          .then(() => {
            this.$message.success("删除菜单成功");
            // 删除成功后，更新数据
            this.getList();
          })
          .catch(() => {
            this.$message.error("删除菜单失败！");
          });
      }
    },
  },
};
</script>

<style scoped lang="scss">
.el-switch {
  width: 40px;
}
.imput {
  width: 50%;
}
</style>
